package zw_1001_1100.zw_1046_最后一块石头的重量;

import java.util.PriorityQueue;

class Solution {
    public static void main(String[] args) {
        int[] stones = {2,7,4,1,8,1};
        int weight = lastStoneWeight(stones);
        System.out.println(weight);
    }

    public static int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
        for (int stone : stones) {
            pq.offer(stone);
        }
        while (pq.size() > 1) {
            int a = pq.poll();
            int b = pq.poll();
            if (a > b) {
                pq.offer(a - b);
            }
        }
        return pq.isEmpty() ? 0 : pq.poll();
    }
}
